package com.emergency.engine.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.emergency.base.domain.Plan;
import com.emergency.common.util.CommonUtils;

import com.emergency.common.util.IdWorker;
import com.emergency.engine.dao.PlanScoreDao;
import com.emergency.engine.dao.ScenarPartDao;
import com.emergency.engine.dao.TimeLineDao;
import com.emergency.engine.domain.PlanScore;
import com.emergency.engine.domain.ScenarPart;
import com.emergency.engine.domain.TimeLine;
import com.emergency.engine.service.SchemeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @Author lijie
 */
@Service
public class SchemeServiceImpl implements SchemeService {

	private static final Logger logger = LoggerFactory.getLogger(SchemeService.class);

	@Autowired
	private ScenarPartDao scenarPartDao;

	@Autowired
	private TimeLineDao timeLineDao;

	@Autowired
	private PlanScoreDao planScoreDao;

	@Autowired
	private IdWorker idWorker;

	@Autowired
	private RedisTemplate redisTemplate;

    @Override
	public List deductionPlan(Long scenarId, List<Plan> planList) {

    	//创建List类型ids存储id值
        List<Long> ids = new ArrayList<>();


    	//按照方案内容，进行推演，推演之后把数据更新
        if (!CommonUtils.isNullOrEmpty(planList)){

			planList.forEach(plan -> {
				//通过查询scenarId和planId的得到需要打分的方案
				PlanScore planScore = planScoreDao.findAllByPlanAndScenarId(scenarId,plan.getId());
				//如果方案为空则插入方案明细
					if (CommonUtils.isNullOrEmpty(planScore)) {
						planScore = new PlanScore();
						planScore.setId(idWorker.nextId());
						planScore.setPlan(plan);
					}

				//如果方案不为空直接为方案打分
				planScore.setPScore(calPlanScore());

				//保存打分对象
				planScoreDao.save(planScore);

				//将所有方案Id添加到List
				ids.add(plan.getId());
			});

		}

        //返回排序后List
		return planScoreDao.findAllByPlanScore(ids);

	}

	/**
	 * 计算方案分值
	 */

	private double calPlanScore(){
		/**
		 * 分值计算方案
		 *产生随机数进行测试
		 *
		 *
		 */

		return Math.random();
	}


	@Override
	public List<ScenarPart> getProgressPart(Long scenarId,Integer type) {
		if (CommonUtils.isNullOrEmpty(type)) {
			type = 0;
		}
		//序列化字符串
		redisTemplate.setKeySerializer(new StringRedisSerializer());
		List<ScenarPart> curScenarParts=null;
		//得到缓存中的推演片段
		List<ScenarPart> scenarParts = (List<ScenarPart>) redisTemplate.opsForValue().get("allScenar-"+scenarId);
		if (CommonUtils.isNullOrEmpty(scenarParts)) {
			scenarParts = scenarPartDao.findAllByScenarId(scenarId);
			//将数据库查询出来的数据放入Redis
			if (!CommonUtils.isNullOrEmpty(scenarParts)) {
				redisTemplate.opsForValue().set("allScenar-" + scenarId, scenarParts);
			}
		}

		// 如片段不为空，获取当前推演进度查询当前片段进度，并获取当前进度；
		if (!CommonUtils.isNullOrEmpty(scenarParts)) {
				logger.info("推演片段开始更新");
				//通过时间轴查询并获取当前场景进度值
				TimeLine timeLine = timeLineDao.findByScenarId(scenarId);
				//获取当前进度值
				Long curTime = timeLine.getProgress();
				//调用片段时间内想定并返回
				curScenarParts = scenarPartDao.findByScenarAndTime(scenarId,curTime,type);


		}
		return curScenarParts;
	}
}
